package ua.com.minus1.lookbattle;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import ua.com.minus1.lookbattle.utils.BundleValues;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * User: Flash
 * Date: 01.10.12
 * Time: 10:49
 * To change this template use File | Settings | File Templates.
 */
public class LookDetailsActivity extends SherlockFragmentActivity {

    private ViewPager mViewPager;
    int mCatId;
    int[] mLooksIds;
    int mLookId;
    private DetailsPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_look_details);

        int mPosition = 0;

        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            mCatId = extras.getInt(BundleValues.CAT_ID);
            mLooksIds = extras.getIntArray(BundleValues.IDS);
            mLookId = extras.getInt(BundleValues.LOOK_ID);
            mPosition = extras.getInt(BundleValues.POSITION);
        }
        //Log.d(LookDetailsActivity.class.getName(), " cat id: " + mCatId + " look id: " + mLookId + " look ids " + mLooksIds + " current item id is: " + Arrays.asList(mLooksIds).indexOf(mLookId));
        setupView();
        mViewPager.setCurrentItem(mPosition);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void setupView() {
        mViewPager = (ViewPager) findViewById(R.id.look_details_pager);
        mAdapter = new DetailsPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public class DetailsPagerAdapter extends FragmentStatePagerAdapter{
        public DetailsPagerAdapter(FragmentManager fm) {
            super(fm);    //To change body of overridden methods use File | Settings | File Templates.
        }

        @Override
        public Fragment getItem(int i) {
            LookDetailsFragment f = LookDetailsFragment.newInstance(mCatId, mLooksIds[i]);
            return f;
        }

        @Override
        public int getCount() {
            return mLooksIds.length;  //To change body of implemented methods use File | Settings | File Templates.
        }
    }
}
